package com.xh3140.xwidget.dialog.view;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

import java.util.Objects;

@SuppressWarnings("unused")
public abstract class RootView<V extends View> extends LinearLayoutCompat {
  private final HeaderView mHeaderView;
  private final V mBodyView;
  private final FooterView mFooterView;

  public RootView(@NonNull Context context) {
    super(context);
    setOrientation(VERTICAL);
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    mHeaderView = createHeaderView(context);
    if (!isGoneHeaderView()) {
      addView(mHeaderView);
    }
    mBodyView = createBodyView(context);
    if (!isGoneBodyView()) {
      addView(mBodyView);
    }
    mFooterView = createFooterView(context);
    if (!isGoneFooterView()) {
      addView(mFooterView);
    }
    setVisibility(isGoneHeaderView() && isGoneBodyView() && isGoneFooterView() ? GONE : VISIBLE);
  }

  /**
   * 创建首部视图
   * 一些特殊的对话框不需要首部视图，直接返回null即可。
   */
  @Nullable
  protected abstract HeaderView createHeaderView(@NonNull Context context);

  /**
   * 创建主体视图
   * 一些特殊的对话框不需要主体视图，直接返回null即可。
   */
  @Nullable
  protected abstract V createBodyView(@NonNull Context context);

  /**
   * 创建底部视图
   * 一些特殊的对话框不需要底部视图，直接返回null即可。
   */
  @Nullable
  protected abstract FooterView createFooterView(@NonNull Context context);

  /**
   * 只在FootView上方加分割线
   */
  @Override
  protected boolean hasDividerBeforeChildAt(int childIndex) {
    final int childCount = getChildCount();
    final int showDividers = getShowDividers();
    if (showDividers < 0) {
      return childIndex > 0 && childCount > 1 && getChildAt(childIndex) == mFooterView;
    }
    if (childIndex == 0) {
      return (showDividers & SHOW_DIVIDER_BEGINNING) != 0;
    } else if (childIndex == childCount) {
      return (showDividers & SHOW_DIVIDER_END) != 0;
    } else if ((showDividers & SHOW_DIVIDER_MIDDLE) != 0) {
      for (int i = childIndex - 1; i >= 0; --i) {
        if (getChildAt(i).getVisibility() != GONE) {
          return true;
        }
      }
    }
    return false;
  }

  @Nullable
  public HeaderView getHeaderView() {
    return mHeaderView;
  }

  @NonNull
  public HeaderView requireHeaderView() {
    return Objects.requireNonNull(mHeaderView, "RootView<?> " + this + " does not have a HeaderView.");
  }

  @Nullable
  public V getBodyView() {
    return mBodyView;
  }

  @NonNull
  public V requireBodyView() {
    return Objects.requireNonNull(mBodyView, "RootView<?> " + this + " does not have a BodyView.");
  }

  @Nullable
  public FooterView getFooterView() {
    return mFooterView;
  }

  @NonNull
  public FooterView requireFooterView() {
    return Objects.requireNonNull(mFooterView, "RootView<?> " + this + " does not have a FooterView.");
  }

  public boolean isGoneHeaderView() {
    return mHeaderView == null || mHeaderView.getVisibility() == GONE;
  }

  public boolean isGoneBodyView() {
    return mBodyView == null || mBodyView.getVisibility() == GONE;
  }

  public boolean isGoneFooterView() {
    return mFooterView == null || mFooterView.getVisibility() == GONE;
  }
}
